<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Logic\DeptLogic;
use Illuminate\Http\Request;

/**
 * Class DeptController
 * @package App\Http\Controllers\OA
 * @desc 部门相关操作
 */
class DeptController extends Controller
{
    //部门列表
    public function deptList(Request $request)
    {
        $rules = [
            "id"=>"present|numeric|nullable",
            "key"=>"present|string|max:5|nullable",
            "name"=>"present|string|nullable",
            "page"=>"required|numeric",
            "pageNum"=>"required|numeric"
        ];

        $this->verify($request->all(),$rules);
        try{
           $logic = new DeptLogic();
           $list = $logic->deptList($request->all());
           ret_json('ok','ok',$list);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }
    //编辑部门
    public function editDept(Request $request)
    {
        $rules = [
            "id"=>"sometimes|numeric",
            "key"=>"sometimes|string|max:5",
            "name"=>"sometimes|string",
            "state"=>"sometimes|numeric"
        ];

        $this->verify($request->all(),$rules);
        try{
            $logic = new DeptLogic();
            $logic->editDept($request->all());
            ret_json('ok','修改成功');
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //新增部门
    public function addDept(Request $request)
    {
        $rules = [
            "key"=>"required|string|max:5",
            "name"=>"required|string",
            "state"=>"required|numeric"
        ];

        $this->verify($request->all(),$rules);
        try{
            $logic = new DeptLogic();
            $lastId = $logic->addDept($request->all());
            ret_json('ok','新增成功',$lastId);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }
}
